//
//  AppDelegate.m
//  滴滴快拍
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "Header.h"
#import <ALBBQuPaiPlugin/ALBBQuPaiPlugin.h>
#import <BmobSDK/Bmob.h>
#import "UIView+TYLaunchAnimation.h"
#import "TYLaunchFadeScaleAnimation.h"
#import "UIImage+TYLaunchImage.h"
#import "TAdLaunchImageView.h"





@interface WBBaseRequest ()
- (void)debugPrint;
@end

@interface WBBaseResponse ()
- (void)debugPrint;
@end

@interface AppDelegate ()

@end

@implementation AppDelegate
@synthesize wbtoken;
@synthesize wbCurrentUserID;
@synthesize wbRefreshToken;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:@"3999765924"];
    
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.window makeKeyAndVisible];
    // 首页
    HomePageViewController *homePage = [[HomePageViewController alloc]init];
    UINavigationController *homeVC = [[UINavigationController alloc]initWithRootViewController:homePage];
    homeVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"Homepage_off.png"] selectedImage:[UIImage imageNamed:@"Homepage.png"]];
    
    FJC_HotListViewController *hotList = [[FJC_HotListViewController alloc]init];
    UINavigationController *hotVC = [[UINavigationController alloc]initWithRootViewController:hotList];
    hotVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"热榜" image:[UIImage imageNamed:@"HotList_off.png"] selectedImage:[UIImage imageNamed:@"HotList.png"]];
    
    UpLoadViewController *up = [[UpLoadViewController alloc]init];
    UINavigationController *upVC = [[UINavigationController alloc]initWithRootViewController:up];
    upVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"录制" image:[UIImage imageNamed:@"Unknown.png"] selectedImage:[UIImage imageNamed:@"Unknown.png"]];
    
    
    FindViewController *find = [[FindViewController alloc]init];
    UINavigationController *findVC = [[UINavigationController alloc]initWithRootViewController:find];
    findVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"Find_off.png"] selectedImage:[UIImage imageNamed:@"Find.png"]];
    

    
    MeViewController *me = [[MeViewController alloc]init];
    UINavigationController *meVC = [[UINavigationController alloc]initWithRootViewController:me];
    meVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"Me_off.png"] selectedImage:[UIImage imageNamed:@"Me.png"]];
    
    
    UITabBarController *tab = [[UITabBarController alloc]init];
    tab.viewControllers = @[homeVC, hotVC, upVC, findVC,meVC];
    
//    tab.tabBar.translucent = NO;
    tab.selectedIndex = 0;
//    tab.tabBar.barTintColor = [UIColor lightGrayColor];
    tab.tabBar.tintColor = [UIColor colorWithRed:244/255.0 green:159/255.0 blue:67/255.0 alpha:1];
      self.window.rootViewController = tab;
    
#pragma mark 趣拍SDK
    [[QupaiSDK shared] registerAppWithKey:QP_appKey secret:QP_appSecret space:@"aglqupai2uuid" success:^(NSString *accessToken) {
        NSLog(@"趣拍%@",accessToken);
        [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:@"accessToken"];
    } failure:^(NSError *error) {
        NSLog(@"初始化失败");
        NSLog(@"%@", error);
    }];
    
#pragma mark BmobSDK
    [Bmob registerWithAppKey:@"17391b159f6a1cbbf2bc3111d944d8ea"];
    
    
#pragma mark 启动广告
    // Ad(广告) FadeAnimation
    TAdLaunchImageView *adLaunchImageView = [[TAdLaunchImageView alloc]initWithImage:[UIImage ty_getLaunchImage]];
    adLaunchImageView.URLString = @"http://ww3.sinaimg.cn/orj480/006h0M0Tjw1f4dprs2wgfj307g0dcmx6.jpg";
    NSLog(@"%@", [UIImage ty_getLaunchImage]);
    adLaunchImageView.backgroundColor = [UIColor redColor];
    // 显示imageView
    [adLaunchImageView showInWindowWithAnimation:[TYLaunchFadeScaleAnimation fadeAnimationWithDelay:5.0] completion:^(BOOL finished) {
        
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        NSLog(@"finished");
    }];
    
    __typeof (self) __weak weakSelf = self;
    // 点击广告block
    [adLaunchImageView setClickedImageURLHandle:^(NSString *URLString) {
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        [weakSelf pushAdViewCntroller];
        NSLog(@"clickedImageURLHandle");
    }];

    
    return YES;
}



- (void)pushAdViewCntroller
{
    // 获取到navVC
    UITabBarController *tabbBarVC = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navVC = tabbBarVC.viewControllers.firstObject;
    
    // 你要推出的VC
     VideoDetailViewController *videoVC= [[VideoDetailViewController alloc]init];
//    VC.view.backgroundColor = [UIColor redColor];
//    *scid;
//     NSURL *videoURL;
    videoVC.videoURL = [NSURL URLWithString:@"http://gslb.miaopai.com/stream/XXmVgKtihbQK34v0YWo8vw__.mp4"];
    videoVC.scid = @"XXmVgKtihbQK34v0YWo8vw__";
    videoVC.hidesBottomBarWhenPushed = YES;
    [navVC pushViewController:videoVC animated:YES];
    videoVC.hidesBottomBarWhenPushed = NO;
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {

}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
    if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
    {
        if (response.statusCode == 0) {
            NSString *title = @"提示";
            NSString *message = @"分享成功";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
        }else if (response.statusCode != 0) {
            NSString *title = @"提示";
            NSString *message = @"分享失败";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
            
            
        }
        

        WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
        NSString* accessToken = [sendMessageToWeiboResponse.authResponse accessToken];
        if (accessToken)
        {
            self.wbtoken = accessToken;
        }
        NSString* userID = [sendMessageToWeiboResponse.authResponse userID];
        if (userID) {
            self.wbCurrentUserID = userID;
        }
       
        
    }
    else if ([response isKindOfClass:WBAuthorizeResponse.class])
    {
        self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
        self.wbCurrentUserID = [(WBAuthorizeResponse *)response userID];
        self.wbRefreshToken = [(WBAuthorizeResponse *)response refreshToken];
        
        NSMutableDictionary* extraParaDict = [NSMutableDictionary dictionary];
        [extraParaDict setObject:@"2" forKey:@"cursor"];
        [extraParaDict setObject:@"3" forKey:@"count"];
        
        [WBHttpRequest requestForUserProfile:self.wbCurrentUserID withAccessToken:self.wbtoken andOtherProperties:nil queue:nil withCompletionHandler:^(WBHttpRequest *httpRequest, WeiboUser * result, NSError *error) {
            
            NSLog(@"%@",result.screenName);
            [self.delegate sendValue:result];
        }];
        
        
        if (response.statusCode == 0) {
            NSString *title = @"提示";
            NSString *message = @"成功";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
        }else if (response.statusCode != 0) {
            NSString *title = @"提示";
            NSString *message = @"失败";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
            
            
        }
        
        
     
       
    }
    else if ([response isKindOfClass:WBPaymentResponse.class])
    {
        if (response.statusCode == 0) {
            NSString *title = @"提示";
            NSString *message = @"分享成功";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
        }else if (response.statusCode != 0) {
            NSString *title = @"提示";
            NSString *message = @"分享失败";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
            
            
        }
        
    }
    else if([response isKindOfClass:WBSDKAppRecommendResponse.class])
    {
        if (response.statusCode == 0) {
            NSString *title = @"提示";
            NSString *message = @"分享成功";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
        }else if (response.statusCode != 0) {
            NSString *title = @"提示";
            NSString *message = @"分享失败";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
            
            
        }
        

    }else if([response isKindOfClass:WBShareMessageToContactResponse.class])
    {
//        NSString *title = NSLocalizedString(@"发送结果", nil);
//        NSString *message = [NSString stringWithFormat:@"%@: %d\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode, NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil),response.requestUserInfo];
        
        if (response.statusCode == 0) {
            NSString *title = @"分享";
            NSString *message = @"分享成功";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];
        }else if (response.statusCode != 0) {
            NSString *title = @"提示";
            NSString *message = @"分享失败";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                  otherButtonTitles:nil];
            [alert show];

        
        }
        
        WBShareMessageToContactResponse* shareMessageToContactResponse = (WBShareMessageToContactResponse*)response;
        NSString* accessToken = [shareMessageToContactResponse.authResponse accessToken];
        if (accessToken)
        {
            self.wbtoken = accessToken;
        }
        NSString* userID = [shareMessageToContactResponse.authResponse userID];
        if (userID) {
            self.wbCurrentUserID = userID;
        }
        
    }
}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WeiboSDK handleOpenURL:url delegate:self ];
}


-(void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result
{
    
    
    NSData *data=[result dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];                //打印接收到的数据，可以根据需要而从字典里取出来
   
}







@end
